package com.ruoyi.system.ali;

import com.ruoyi.common.enums.ChannelEnum;
import com.ruoyi.common.utils.sign.SignMD5Util;
import com.ruoyi.system.biz.CashBizService;
import com.ruoyi.system.domain.Bank;
import com.ruoyi.system.domain.CashHistory;
import com.ruoyi.system.domain.Order;
import com.ruoyi.system.domain.UserRate;
import com.ruoyi.system.dto.ApiByPhoneAddDto;
import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.ApiPayResultVo;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.TreeMap;

/**
 * 银行卡通道
 */
//@Component
public class BankPayService implements ChannelService {
    @Resource
    ISysConfigService configService;
    private static final String serverUrl = "https://openapi.alipay.com/gateway.do";
    @Resource
    private ZfbConfigService zfbConfigService;
    @Resource
    private BankService bankService;
    @Resource
    private UserRateService userRateService;

    @Override
    public String getType() {
        //  return ChannelEnum.BANK.getCode();
        return "10003";
    }

    @Resource
    private CashBizService cashService;
    @Resource
    private ISysUserService userService;

    public Object create(AliPayDto aliPayDto, Order order) {
        Bank bank = bankService.getRand(order.getAmount(), "bank");
        return create(order, bank);
    }

    @Override
    public Object createByPhone(ApiByPhoneAddDto apiByPhoneAddDto, Order order) {
        Bank bank = bankService.getUserBank(order.getUserId(), order.getAmount(), getType());
        return create(order, bank);
    }

    //uid
    public Object create(Order order, Bank bank) {
        BigDecimal payAmount = order.getAmount();
        //冻结收款用户的金额
        CashHistory userCashHis = new CashHistory(bank.getUserId(), order.getAmount(), "reduce", "代收订单", "freeze");
        userCashHis.setOrderNo(order.getOrderNo());
        cashService.updateCash(bank.getUserId(), userCashHis);
        order.setChannelCode(this.getType());
        order.setChannelName(ChannelEnum.getName(this.getType()));
        order.setAccount(bank.getCardNo());
        order.setName(bank.getName());
        order.setType("hand");
        order.setBankName(bank.getBankName());
        order.setUserId(bank.getUserId());
        order.setUsername(bank.getUsername());
        UserRate userRate = userRateService.checkUserRate(bank.getUserId(), this.getType());
        order.setEarning(order.getAmount().multiply(userRate.getRate().divide(new BigDecimal(100))));
        order.setUsername(bank.getUsername());
        ApiPayResultVo apiVo = new ApiPayResultVo();
        apiVo.setAmount(String.valueOf(payAmount));
        apiVo.setOrderNo(order.getOrderNo());
        apiVo.setMerchantId(order.getMerchantId());
        TreeMap<String, String> map = SignMD5Util.objToMap(apiVo);
        //3 银行卡
        map.put("payType", "3");
        String sign = SignMD5Util.getSign(map, "sign");
        String payUrl = configService.selectConfigByKey("template");
        payUrl = payUrl + "?" + SignMD5Util.getStr(map, sign);
        apiVo.setUrl(payUrl);
        return apiVo;
    }


}
